//用户模块
//引入express模块
const express = require("express");

//引入pool连接池模块
const pool =require("../pool.js");
//创建路由对象
const router = express.Router();
//所有路由
//*************预留路由位置 ↓↓↓↓↓************
//1.用户登录接口
//接口地址：http://127.0.0.1:9000/user/login
//请求方式：post
router.post("/login",(req,res,next)=>{
  let obj = req.body;
  let sql = "select * from yms_user where u_phone =? and u_pwd =?;"
  pool.query(sql,[obj.u_phone,obj.u_pwd],(err,result)=>{
    if(err){return next(err)}
    if(result.length){
      res.send({
        code:200,
        msg:"登录成功",
        data:result
      })
    }else{
      res.send({
        code:201,
        msg:"失败,请先注册"
      })
    }
  })
})
//2.用户注册接口
//接口地址：http://127.0.0.1:9000/user/register
//请求方式：post
router.post("/register",(req,res,next)=>{
  let obj = req.body;
  let sql = "insert into yms_user set?;"
  pool.query(sql,[obj],(err,result)=>{
    if(err){return next(err)}
    if(result.affectedRows){
      res.send({
        code:200,
        msg:"注册成功"
      })
    }else{
      res.send({
        code:201,
        msg:"注册失败"
      })
    }
  })
})

// 搜索页获取房间信息路由
router.get('/list', (req, res, next) => {
  var obj=req.query;
  pool.query('SELECT * FROM (yms_address INNER JOIN yms_room ON yms_address.d_number=yms_room.d_number) INNER JOIN yms_room_pic ON yms_room.r_id=yms_room_pic.r_id where d_name=? group by yms_room.r_id;',[obj.dname],(err, data) => {
    if (err) {
      next(err);
      return
    };
    res.send({
      code: 1,
      msg: '返回所有数据',
      data: data
    });
  })
})

//*************预留路由位置 ↑↑↑↑↑↑************
//导出路由对象
module.exports = router;